begincreaturescript;

variables;

short i,target;
short last_abil_time;
short turns_between = 4;
short num_to_make = 1;
short can_spawn = 1;

body;

beginstate INIT_STATE;
	last_abil_time = get_current_tick();
	
	if (get_memory_cell(1) > 0)
		num_to_make = get_memory_cell(1);
	if (get_memory_cell(2) > 0)
		turns_between = get_memory_cell(2);
	break;

beginstate DEAD_STATE;
	message_dialog("The beast howls furiously! It has been mortally wounded and can no longer stay on its feet. It thumps to the ground. You sigh in relief. That was a mighty beast, indeed.","");
	set_flag(9,1,1);
	break;

beginstate START_STATE; 
	if (((get_memory_cell(3) > 0) || (get_memory_cell(4) > 0)) && (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(3),get_memory_cell(4)) >= get_memory_cell(5)) {
			can_spawn = 0;
			if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (select_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}		}}
		
	// Look for a target, maybe spawn critters
	if (select_target(ME,8,0)) {
		if ((dist_to_party() <= 8) && (get_attitude(ME) >= 10) && (tick_difference(last_abil_time,get_current_tick()) >= turns_between) && (can_spawn > 0)) {
			print_str("The alien beast growls and summons its pack to attack you.");
			play_sound(-33);
			i = 0;
			while (i < num_to_make) {
				place_monster(22,16,get_memory_cell(0),1);
				place_monster(20,24,get_memory_cell(0),1);
				place_monster(10,18,get_memory_cell(0),1);
				i = i + 1;
				}
			last_abil_time = get_current_tick();
			if ((get_memory_cell(3) > 0) || (get_memory_cell(4) > 0)) 
				inc_flag(get_memory_cell(3),get_memory_cell(4),1);
			}
		}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;
beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;